package com.atguigu.controller;

import java.io.*;

import com.atguigu.pojo.Delivery;
import com.atguigu.pojo.SysUser;
import com.atguigu.service.DeliveryService;
import com.atguigu.service.impl.DeliveryServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/delivery_system/list")
public class DeliveryListController extends HttpServlet {
    private DeliveryService deliveryService = new DeliveryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
               /*
          2.获取登录的用户id
            我们需要用登录用户的id去查询对应的快递信息
            也就是哪个用户登录,就只查询哪个用户相关的快递信息
         */
            SysUser sysUser = (SysUser) request.getSession().getAttribute("user");
            Integer userId = sysUser.getId();
            List<Delivery> list  = deliveryService.findAllDeliveryByUserId(userId);
            request.setAttribute("deliveryList", list);
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}